package nsqio

import (
	"encoding/json"
	"gitee.com/liukexing/mqbroker/core/driver/logger"
	"gitee.com/liukexing/mqbroker/core/typedef"
)

type NsqResponseHandle struct {
	parent *NsqPubSub
}

func (m *NsqResponseHandle) Praise(v []byte) (interface{}, bool) {
	rsp := typedef.ReplyData{}
	if logger.CheckErr(typedef.ErrInfo{
		Info: Config.Info,
		Err:  json.Unmarshal(v, &rsp),
	}) {
		return nil, true
	}
	return rsp, false
}

func (m *NsqResponseHandle) Process(data interface{}) error {
	rsp := data.(typedef.ReplyData)
	key := rsp.Topic + rsp.ReqID
	if rspchan, ok := m.parent.ReqMap.Load(key); ok {
		rspchan.(chan typedef.ReplyData) <- rsp
	}
	return nil
}
